package inje.edu.cse.chansung.projectmanager.setting;

import inje.edu.cse.chansung.projectmanager.R;
import inje.edu.cse.chansung.projectmanager.common.CommonConstVariableSet;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SettingActivity extends Activity implements OnClickListener {

	private String userId;
	private String userPassword;
	
	private EditText idEditText;
	private EditText passwordEditText;
	
	private Button loginButton;
	private Button signupButton;
	private Button logoutButton;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_layout);
		
		idEditText 			= (EditText)findViewById(R.id.setting_id_text_field);
		passwordEditText 	= (EditText)findViewById(R.id.setting_password_text_field);
		
		loginButton 			= (Button)findViewById(R.id.setting_login_button);
		loginButton.setOnClickListener(this);
		
		signupButton 		= (Button)findViewById(R.id.setting_signup_button);
		signupButton.setOnClickListener(this);
		
		logoutButton 		= (Button)findViewById(R.id.setting_logout_button);
		logoutButton.setOnClickListener(this);
		
		setAuthStateComponents();
	}

	public void onClick(View v) {
		switch( v.getId() ) {
		case R.id.setting_login_button :
			break;
			
		case R.id.setting_signup_button :
			break;
			
		case R.id.setting_logout_button :
			removeUserAuthState();
			break;
		}
		
		setAuthStateComponents();
	}
	
	/*
	 * setAuthStateComponents() method
	 * 
	 * set components state(disabled, enabled) according to 
	 * login auth state
	 */
	public void setAuthStateComponents() {
		//if there is no data logged(to logout) in previously, 
		//logout button is going to be disabled.
		if( !isUserAuthStateSaved() ) {
			logoutButton.setClickable(false);
		} else {
			loginButton.setClickable(false);
			signupButton.setClickable(false);
			idEditText.setEnabled(false);
			passwordEditText.setEnabled(false);
			
			//if logged in state, fill id and password into edit text fields
			getUserAuthState();
			idEditText.setText(userId);
			passwordEditText.setText(userPassword);
		}
	}
	
	/*
	 * isUserStateSaved() method
	 * 
	 * this checks if there is user id and password saved in SharedPreference instance object.
	 */
	public boolean isUserAuthStateSaved() {
		SharedPreferences preferences = getPreferences(MODE_PRIVATE);
		
		String tmpId			= preferences.getString(CommonConstVariableSet.USER_ID, 		
													CommonConstVariableSet.NONE_STRING);
		String tmpPassword	= preferences.getString(CommonConstVariableSet.USER_PASSWORD,
													CommonConstVariableSet.NONE_STRING);
		
		Log.d("debug : ", tmpId);
		Log.d("debug : ", tmpPassword);
		
		if( tmpId.equals(CommonConstVariableSet.NONE_STRING) ||
			tmpPassword.equals(CommonConstVariableSet.NONE_STRING) ) {
			return false;
		}
		
		return true;
	}
	
	/*
	 * getUserAuthState() method
	 * 
	 * if there is a user auth state saved (check this with isUserAuthStateSvaed method)
	 * get it's data into class memeber variables, userId, and userPassword
	 */
	public void getUserAuthState() {
		if( isUserAuthStateSaved() ) {
			SharedPreferences preferences = getPreferences(MODE_PRIVATE);
			userId 			= preferences.getString(CommonConstVariableSet.USER_ID, 		
													CommonConstVariableSet.NONE_STRING);
			userPassword		= preferences.getString(CommonConstVariableSet.USER_PASSWORD,
													CommonConstVariableSet.NONE_STRING);
		}
	}
	
	/*
	 * saveUserAuthState(String, String) method
	 * 
	 * save user id and password into SharedPreferences instance object.
	 */
	public void saveUserAuthState(String userId, String userPassword) {
		SharedPreferences preferences = getPreferences(MODE_PRIVATE);  
		SharedPreferences.Editor editor = preferences.edit();
		
		editor.putString(CommonConstVariableSet.USER_ID, 		userId);
		editor.putString(CommonConstVariableSet.USER_PASSWORD, 	userPassword);
		
		editor.commit();
	}
	
	/*
	 * removeUserAuthState() method
	 * 
	 * remove user auth information ** used for logout button
	 */
	public void removeUserAuthState() {
		if( isUserAuthStateSaved() ) {
			SharedPreferences preferences = getPreferences(MODE_PRIVATE);  
			SharedPreferences.Editor editor = preferences.edit();

			editor.putString(CommonConstVariableSet.USER_ID, 		
							 CommonConstVariableSet.NONE_STRING);
			editor.putString(CommonConstVariableSet.USER_PASSWORD, 	
							 CommonConstVariableSet.NONE_STRING);
			
			editor.commit();
		}
	}
}
